package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.SendCodeSenarioType;
import com.onstar.cn.mag.joinme.RegisterRequestEntity;
import com.onstar.cn.mag.joinme.RegisterToVisitorResponse;
import com.shanghaionstar.login.SupplementPhoneActivity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.JoinMeConstants;
import com.shanghaionstar.utils.NetConnectUtil;

/**
 * Created by 7828wc on 16/7/1.
 */
public class ChangeEmailOrPhoneService extends BaseService<RegisterToVisitorResponse> {
    private Context context;
    private String newEmail, newPhoneNumber, verifyCode = "";

    public ChangeEmailOrPhoneService(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public RequestEntity getRequestEntity() {
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        UsersService publicService = new UsersService(Constants.CURRENT_SERVER_URL);

        RegisterRequestEntity codeEntity = new RegisterRequestEntity();
        codeEntity.setUserName(ApplicationUtil.getInstance().getIdpID());
        codeEntity.setSendCodeSenario(SendCodeSenarioType.CHANGE_MOBILE_EMAIL.value());
        codeEntity.setNewEmailAddress(newEmail);
        codeEntity.setNewMobilePhoneNumber(newPhoneNumber);
        codeEntity.setSourceId(new DeviceUtil(context).getDeviceID());

        if (CommonUtil.isNull(verifyCode)) {
            if (ApplicationUtil.getInstance().isLogin() || context instanceof SupplementPhoneActivity) {
                String loginRole = ApplicationUtil.getInstance().getLoginRole();
                if (!CommonUtil.isNull(loginRole) && loginRole.equalsIgnoreCase(JoinMeConstants.VISITOR)) {
                    requestEntity = publicService.getCodeByLogining(requestEntity, ApplicationUtil.getInstance().getIdpID(), codeEntity);
                } else {
                    requestEntity = publicService.getCodeByLogining(requestEntity, ApplicationUtil.getInstance().getSubscriberID(), codeEntity);
                }
            }
        } else {
            codeEntity.setSecCode(verifyCode);
            publicService.getPhoneAndnumber(requestEntity, codeEntity);
        }

        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }

    public void setUserInfo(String newEmail, String newPhoneNumber, String verifyCode) {
        this.newEmail = newEmail;
        this.newPhoneNumber = newPhoneNumber;
        this.verifyCode = verifyCode;
    }

    @Override
    public RegisterToVisitorResponse jsonToBean(String json) {
        if (!CommonUtil.isNull(json))
            return GsonHelper.fromJson(json,RegisterToVisitorResponse.class);
        else return null;
    }
}
